package com.safeluck.floatwindow.usbVideo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;

/**

 * Created by Administrator on 2017/10/18.
 */

public class ImageUtil {


        /**
         * 设置水印图片在左上角
         * @param Context
         * @param src
         * @param watermark
         * @param paddingLeft
         * @param paddingTop
         * @return
         */
        public static Bitmap createWaterMaskLeftTop(
                Context context, Bitmap src, Bitmap watermark,
                int paddingLeft, int paddingTop) {
            return createWaterMaskBitmap(src, watermark,
                    dp2px(context, paddingLeft), dp2px(context, paddingTop));
        }

        private static Bitmap createWaterMaskBitmap(Bitmap src, Bitmap watermark,
                                                    int paddingLeft, int paddingTop) {
            if (src == null) {
                return null;
            }
            int width = src.getWidth();
            int height = src.getHeight();
            //创建一个bitmap
            Bitmap newb = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);// 创建一个新的和SRC长度宽度一样的位图
            //将该图片作为画布
            Canvas canvas = new Canvas(newb);
            //在画布 0，0坐标上开始绘制原始图片
            canvas.drawBitmap(src, 0, 0, null);
            //在画布上绘制水印图片
            canvas.drawBitmap(watermark, paddingLeft, paddingTop, null);
            // 保存
//            canvas.save(Canvas.ALL_SAVE_FLAG);
            // 存储
            canvas.restore();
            return newb;
        }

        /**
         * 设置水印图片在右下角
         * @param Context
         * @param src
         * @param watermark
         * @param paddingRight
         * @param paddingBottom
         * @return
         */
        public static Bitmap createWaterMaskRightBottom(
                Context context, Bitmap src, Bitmap watermark,
                int paddingRight, int paddingBottom) {
            return createWaterMaskBitmap(src, watermark,
                    src.getWidth() - watermark.getWidth() - dp2px(context, paddingRight),
                    src.getHeight() - watermark.getHeight() - dp2px(context, paddingBottom));
        }

        /**
         * 设置水印图片到右上角
         * @param Context
         * @param src
         * @param watermark
         * @param paddingRight
         * @param paddingTop
         * @return
         */
        public static Bitmap createWaterMaskRightTop(
                Context context, Bitmap src, Bitmap watermark,
                int paddingRight, int paddingTop) {
            return createWaterMaskBitmap( src, watermark,
                    src.getWidth() - watermark.getWidth() - dp2px(context, paddingRight),
                    dp2px(context, paddingTop));
        }

        /**
         * 设置水印图片到左下角
         * @param Context
         * @param src
         * @param watermark
         * @param paddingLeft
         * @param paddingBottom
         * @return
         */
        public static Bitmap createWaterMaskLeftBottom(
                Context context, Bitmap src, Bitmap watermark,
                int paddingLeft, int paddingBottom) {
            return createWaterMaskBitmap(src, watermark, dp2px(context, paddingLeft),
                    src.getHeight() - watermark.getHeight() - dp2px(context, paddingBottom));
        }

        /**
         * 设置水印图片到中间
         * @param Context
         * @param src
         * @param watermark
         * @return
         */
        public static Bitmap createWaterMaskCenter(Bitmap src, Bitmap watermark) {
            return createWaterMaskBitmap(src, watermark,
                    (src.getWidth() - watermark.getWidth()) / 2,
                    (src.getHeight() - watermark.getHeight()) / 2);
        }

        /**
         * 给图片添加文字到左上角
         * @param context
         * @param bitmap
         * @param text
         * @return
         */
        public static Bitmap drawTextToLeftTop(Context context, Bitmap bitmap, String text,
                                               int size, int color, int paddingLeft, int paddingTop) {
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setColor(color);
            paint.setTextSize(dp2px(context, size));
            Rect bounds = new Rect();
            paint.getTextBounds(text, 0, text.length(), bounds);
            return drawTextToBitmap(context, bitmap, text, paint, bounds,
                    dp2px(context, paddingLeft),
                    dp2px(context, paddingTop) + bounds.height());
        }

        /**
         * 绘制文字到右下角
         * @param context
         * @param bitmap
         * @param text
         * @param size
         * @param color
         * @param paddingRight
         * @param paddingBottom
         * @return
         */
        public static Bitmap drawTextToRightBottom(Context context, Bitmap bitmap, String text,
                                                   int size, int color, int paddingRight, int paddingBottom) {
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setColor(color);
            paint.setTextSize(dp2px(context, size));
            Rect bounds = new Rect();
            paint.getTextBounds(text, 0, text.length(), bounds);
            return drawTextToBitmap(context, bitmap, text, paint, bounds,
                    bitmap.getWidth() - bounds.width() - dp2px(context, paddingRight),
                    bitmap.getHeight() - dp2px(context, paddingBottom));
        }

        /**
         * 绘制文字到右上方
         * @param context
         * @param bitmap
         * @param text
         * @param size
         * @param color
         * @param paddingRight
         * @param paddingTop
         * @return
         */
        public static Bitmap drawTextToRightTop(Context context, Bitmap bitmap, String text,
                                                int size, int color, int paddingRight, int paddingTop) {
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setColor(color);
            paint.setTextSize(dp2px(context, size));
            Rect bounds = new Rect();
            paint.getTextBounds(text, 0, text.length(), bounds);
            return drawTextToBitmap(context, bitmap, text, paint, bounds,
                    bitmap.getWidth() - bounds.width() - dp2px(context, paddingRight),
                    dp2px(context, paddingTop) + bounds.height());
        }

        /**
         * 绘制文字到左下方
         * @param context
         * @param bitmap
         * @param text
         * @param size
         * @param color
         * @param paddingLeft
         * @param paddingBottom
         * @return
         */
        public static Bitmap drawTextToLeftBottom(Context context, Bitmap bitmap, String text,
                                                  int size, int color, int paddingLeft, int paddingBottom) {
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setColor(color);
            paint.setTextSize(dp2px(context, size));
            Rect bounds = new Rect();
            paint.getTextBounds(text, 0, text.length(), bounds);
            return drawTextToBitmap(context, bitmap, text, paint, bounds,
                    dp2px(context, paddingLeft),
                    bitmap.getHeight() - dp2px(context, paddingBottom));
        }

        /**
         * 绘制文字到中间
         * @param context
         * @param bitmap
         * @param text
         * @param size
         * @param color
         * @return
         */
        public static Bitmap drawTextToCenter(Context context, Bitmap bitmap, String text,
                                              int size, int color) {
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setColor(color);
            paint.setTextSize(dp2px(context, size));
            Rect bounds = new Rect();
            paint.getTextBounds(text, 0, text.length(), bounds);
            return drawTextToBitmap(context, bitmap, text, paint, bounds,
                    (bitmap.getWidth() - bounds.width()) / 2,
                    (bitmap.getHeight() + bounds.height()) / 2);
        }

        //图片上绘制文字
        private static Bitmap drawTextToBitmap(Context context, Bitmap bitmap, String text,
                                               Paint paint, Rect bounds, int paddingLeft, int paddingTop) {
            Bitmap.Config bitmapConfig = bitmap.getConfig();

            paint.setDither(true); // 获取跟清晰的图像采样
            paint.setFilterBitmap(true);// 过滤一些
            if (bitmapConfig == null) {
                bitmapConfig = Bitmap.Config.RGB_565;
            }
            bitmap = bitmap.copy(bitmapConfig, true);
            Canvas canvas = new Canvas(bitmap);

            canvas.drawText(text, paddingLeft, paddingTop, paint);
            return bitmap;
        }

        /**
         * 缩放图片
         * @param src
         * @param w
         * @param h
         * @return
         */
        public static Bitmap scaleWithWH(Bitmap src, double w, double h) {
            if (w == 0 || h == 0 || src == null) {
                return src;
            } else {
                // 记录src的宽高
                int width = src.getWidth();
                int height = src.getHeight();
                // 创建一个matrix容器
                Matrix matrix = new Matrix();
                // 计算缩放比例
                float scaleWidth = (float) (w / width);
                float scaleHeight = (float) (h / height);
                // 开始缩放
                matrix.postScale(scaleWidth, scaleHeight);
                // 创建缩放后的图片
                return Bitmap.createBitmap(src, 0, 0, width, height, matrix, true);
            }
        }

        /**
         * dip转pix
         * @param context
         * @param dp
         * @return
         */
        public static int dp2px(Context context, float dp) {
            final float scale = 1.6f;//context.getResources().getDisplayMetrics().density;

//            Log.d("DEBUG", "拍照成功 scale " + scale);

            return (int) (dp * scale + 0.5f);
        }



    public static byte[] rgb2YCbCr420(int[] pixels, int width, int height) {
        int len = width * height;
        // yuv格式数组大小，y亮度占len长度，u,v各占len/4长度。
        byte[] yuv = new byte[len * 3 / 2];
        int y, u, v;
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                // 屏蔽ARGB的透明度值
                int rgb = pixels[i * width + j] & 0x00FFFFFF;
                // 像素的颜色顺序为bgr，移位运算。
                int r = rgb & 0xFF;
                int g = (rgb >> 8) & 0xFF;
                int b = (rgb >> 16) & 0xFF;
                // 套用公式
                y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
                u = ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
                v = ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
                // rgb2yuv
                // y = (int) (0.299 * r + 0.587 * g + 0.114 * b);
                // u = (int) (-0.147 * r - 0.289 * g + 0.437 * b);
                // v = (int) (0.615 * r - 0.515 * g - 0.1 * b);
                // RGB转换YCbCr
                // y = (int) (0.299 * r + 0.587 * g + 0.114 * b);
                // u = (int) (-0.1687 * r - 0.3313 * g + 0.5 * b + 128);
                // if (u > 255)
                // u = 255;
                // v = (int) (0.5 * r - 0.4187 * g - 0.0813 * b + 128);
                // if (v > 255)
                // v = 255;
                // 调整
                y = y < 16 ? 16 : (y > 255 ? 255 : y);
                u = u < 0 ? 0 : (u > 255 ? 255 : u);
                v = v < 0 ? 0 : (v > 255 ? 255 : v);
                // 赋值
                yuv[i * width + j] = (byte) y;
                yuv[len + (i >> 1) * width + (j & ~1) + 0] = (byte) u;
                yuv[len + +(i >> 1) * width + (j & ~1) + 1] = (byte) v;
            }
        }
        return yuv;
    }

    /*
     * 获取位图的YUV数据
     */
    public static byte[] getYUVByBitmap(Bitmap bitmap) {
        if (bitmap == null) {
            return null;
        }
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        int size = width * height;

        int pixels[] = new int[size];
        bitmap.getPixels(pixels, 0, width, 0, 0, width, height);

        // byte[] data = convertColorToByte(pixels);
        byte[] data = rgb2YCbCr420(pixels, width, height);

        return data;
    }


}
